<?php
//------------------------
// 前台订单控制器
//-------------------------

namespace app\home\controller;

use app\common\controller\HomeBase;
use app\common\model\Cart as CartModel;
use app\common\model\Order as OrderModel;
use app\common\model\UserAddress as UserAddressModel;
use think\Session;
use think\Cookie;
use constant\OrderStatus;
use constant\PayStatus;
use constant\ShippingStatus;



class Order extends HomeBase
{
    protected $CartModel;
    protected $OrderModel;
    protected $UserAddressModel;




	protected function _initialize()
    {
        parent::_initialize();
        $this->CartModel              = new CartModel();
        $this->OrderModel             = new OrderModel();
        $this->UserAddressModel             = new UserAddressModel();

    }

    // 下订单页面
    public function index(){
        $data            = $this->request->param();

        if( empty($data['buythis']) ){
            $buythis = Session::get('buythis');

            if(!$buythis)
                $this->error('请勾选需要购买的商品',url('home/Cart/index') );
        } else {
            Session::set('buythis', $data['buythis']);
        }

        $user_id = Session::get('user_id');

        if ($user_id) {
            $addressInfo = $this->UserAddressModel->where('user_id',$user_id)->select();
            $this->assign('addressInfo',$addressInfo);

            $cartInfo = $this->CartModel->getCartInfo();

            // 获取购物车中勾选了的商品
            $res = $this->CartModel->getCartToBuyThis($cartInfo,Session::get('buythis'));

            $this->assign('buyThisInfo',$res['info']);
            $this->assign('totalPrice',$res['totalPrice']);
        }

        return $this->fetch('index');
    }

    // 下单处理
    public function orderProc(){
        $data            = $this->request->param();

        $validate_result = $this->validate($data, 'Order.add');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        // 下单操作
        $order_id = $this->OrderModel->placeOrder($data);

        if ($order_id === false) {
            $this->error($this->OrderModel->getError());
        } else {
            $this->success($order_id);
        }
    }

    // 选择付款方式
    public function optionPayment(){
        $id = $this->request->param('id',0,'intval');//订单id

        if (!$id) {
            $this->error('非法操作');
        }

        $orderInfo = $this->OrderModel->where('id',$id)->field('id,order_sn,total_amount,add_time')->find()->toArray();

        // halt($orderInfo);
        $this->assign('orderInfo',$orderInfo);
        return $this->fetch('payment');
    }

    // 订单详情
    public function orderDetails(){
        $id = $this->request->param('id',0,'intval');//订单id
        halt($id);
        return $this->fetch('order_details');
    }

    // 货到付款处理
    public function codProc(){
        $order_id = $this->request->param('order_id',0,'intval');//订单id

        $data = array();

        if ($order_id) {
            $data = array(
                'order_status' => OrderStatus::CONFIRMED,
                'pay_name'     => 'cod_pay'
            );
        }

        $res = $this->OrderModel->where('id',$order_id)->update($data);

        if ($res) {
            $this->success('请你保持手机通畅,我们将在第一时间给你发货！');
        } else {
            $this->error('非法操作');
        }
    }

    // 微信支付处理
    public function wxProc(){
        action('wechat/Pay/orderPay');
    }





}
